
import Message from "@/utils/Message"
import axios from "axios";
import store from "@/store";
import { ElLoading } from "element-plus";

const contentTypeForm = 'application/x-www-form-urlencoded;charset=UTF-8';
const contentTypeJson = 'application/json;charset=UTF-8';
const instance = axios.create({
  baseURL: "/api",
  timeout: 5000,
});

//请求前过滤器
let loading = null;
instance.interceptors.request.use((config)=>{
  if(config.showLoading){
    loading = ElLoading.service({
      lock: true,
      text: 'Loading...',
      background: 'rgba(0, 0, 0, 0.7)'
    });
  }
  return config;
},(error)=>{
  if(error.config.showLoading && loading){
    loading.close();
  }
  Message.error("请求失败");
  return Promise.reject("请求失败");
});

//请求后过滤器
instance.interceptors.response.use((response)=>{
  const  {showLoading,errorCallback,showError} = response.config;
  if(showLoading && loading){
    loading.close();
  }

  const responseData = response.data;;
  if(response.data.code == 200){
    return responseData;
  }else if(responseData.code == 901){
    Message.error("登录超时");
    store.commit("showLogin",true)
    store.commit('updateLoginUserInfo',null)
    return Promise.reject({showError:false,msg:"登录超时"});
  }else{
    if(errorCallback){
      errorCallback(responseData)
    }
    console.log(showError,"错误信息");
    console.log(responseData.info,"错误信息2");
    Message.error(responseData.info);
    return Promise.reject({showError:showError,msg:responseData.info});
  }

},(error)=>{
  if(error.config.showLoading && loading){
    loading.close();
  }
  return Promise.reject({showError : true,msg:"网络异常"});
});

const request = (config)=>{
  const {url,params,dataType,showLoading = true, errorCallback,showError = true} = config

  let contentType = contentTypeForm;
  let fromData = new FormData();
  for(let key in params){
    fromData.append(key,params[key] == undefined ?'':params[key]);
  }

  if(dataType != null && dataType === 'json'){
    contentType = contentTypeJson;
  }
  let headers = {
    'Content-Type': contentType,
    'X-Requested-With': 'XMLHttpRequest',
  }
  return instance.post(url,fromData,{
    headers: headers,
    showLoading: showLoading,
    errorCallback: errorCallback,
    showError: showError
  }).catch(error=>{
    if(errorCallback && errorCallback.showError){
      Message.error(error.msg);
    }
    return null;
  })

}

export default request;